var http = require('http');
var express = require("express");
var RED = require("node-red");
var path = require('path');

// Create an Express app
var app = express();

// Add a simple route for static content served from 'public'
app.use("/", express.static("public"));

// Create a server
var server = http.createServer(app);

// Create the settings object - see default settings.js file for other options
/**
 * @type {import('@node-red/runtime').LocalSettings}
 */
var settings = {
    flowFile: path.resolve(__dirname, '.nodered', 'flows.json'),
    httpAdminRoot: "/",
    httpNodeRoot: "/api/:flowId",
    userDir: path.resolve(__dirname, '.nodered'),
    functionGlobalContext: {},    // enables global context
    editorTheme: {
        page: {
            title: "物联网驱动",
            css: path.resolve(__dirname, 'public', 'node-red-custom.css'),
            scripts: path.resolve(__dirname, 'public', 'node-red-custom.js')
        },
        header: {   
            title: "物联网驱动",
        }
    }
}

// Initialise the runtime with a server and settings
RED.init(server, settings);

app.use(settings.httpAdminRoot, (req, res, next) => {
    return RED.httpAdmin(req, res, next);
});

// Serve the http nodes UI from /api
app.use(settings.httpNodeRoot, (req, res, next) => {
    return RED.httpNode(req, res, next);
});

server.listen(8000);

// Start the runtime
RED.start();